package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jiayen.cache.FileCacheManager;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.ProductInterfaceRightMenuAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.ProductUIMenuModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/7/28.
 */

public class AdjustSortImageTypeActivity extends SlidingActivity {

	private int action;
	private List<ProductUIMenuModel> data;
	private ProductInterfaceRightMenuAdapter adapter;

	private String cachePath;
	private FileCacheManager cacheManager;

	@ViewInject(R.id.contentRv)
	private RecyclerView contentRv;

	@Override
	protected int getResourceId() {
		return R.layout.activity_adjust_sort_image_type;
	}

	@Override
	protected void init() {
		action = getIntent().getIntExtra("action", 1);
		topName.setText("选择分类");
		leftIcon.setImageResource(R.mipmap.guanzhufanhui);
		rightIcon.setVisibility(View.GONE);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		contentRv.setHasFixedSize(true);
		contentRv.addItemDecoration(new JpDiver(mContext));
		contentRv.setLayoutManager(new LinearLayoutManager(mContext));
		data = new ArrayList<>();
		adapter = new ProductInterfaceRightMenuAdapter(mContext, data);
		contentRv.setAdapter(adapter);
		adapter.setSelectedPosition(-1);
		adapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				setResult(RESULT_OK, new Intent()
						.putExtra("tagId", data.get(position).getId())
						.putExtra("name", data.get(position).getName()));
				onBackPressed();
			}
		});
		cacheManager = new FileCacheManager();
		cachePath = mContext.getCacheDir() + "/productInterfaceRightMenu_" + action + ".dat";
		readCache();
	}

	private void readCache(){
		Observable.create(new Observable.OnSubscribe<List<ProductUIMenuModel>>() {
			@Override
			public void call(Subscriber<? super List<ProductUIMenuModel>> subscriber) {
				List<ProductUIMenuModel> data = (List<ProductUIMenuModel>) cacheManager.readObject(cachePath);
				subscriber.onNext(data);
				subscriber.onCompleted();
			}
		}).subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<List<ProductUIMenuModel>>() {
					@Override
					public void onCompleted() {}
					@Override
					public void onError(Throwable e) {
						onCompleted();
					}
					@Override
					public void onNext(List<ProductUIMenuModel> data) {
						AdjustSortImageTypeActivity.this.data.clear();
						if(data != null && data.size() > 0) {
							if(data.get(0).getId() == null)
								data.remove(0);
							AdjustSortImageTypeActivity.this.data.addAll(data);
						}
						adapter.updateAdapter(AdjustSortImageTypeActivity.this.data);
					}
				});
	}
}
